package com.atguigu.mr.combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @description:
 * @create: 2019-12-15 13:16
 * @author: baojinlong
 **/
public class WordcountDriver {
    public static void main(String[] args) throws Exception {
        // 1.获取job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        // 2.设置jar存储位置
        job.setJarByClass(WordcountDriver.class);
        // 3.关联mapper和reducer
        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReduce.class);
        // 4.这是mapper输出的key和value类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        //5.设置最终输出的key和value类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 5.1如果不设置InputFormat默认使用的是TextInputFormat.class,下面两个可以不加按照默认切片方式
        job.setInputFormatClass(CombineTextInputFormat.class);
        // 设置虚拟存储切片最大值4M normal:20M 或者har压缩
        CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);

        // 指定需要使用combiner，以及用哪个类作为combiner的逻辑
        job.setCombinerClass(WordcountCombiner.class);
        // 这两个类里面逻辑是一样的,所以可以直接使用Reduce来实现即可
        job.setCombinerClass(WordcountReduce.class);

        // 6.设置输入和输出数据的路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // 7.提交作业 不建议submit();
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
